﻿/*                              
   Copyright 2011 Nils Kopal, Christopher Krüger, Simone Sauer, Dennis Nolte, Uni Duisburg-Essen

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sharebert.SearchLibrary.Interfaces;
using System.Net;
using System.Net.Sockets;

namespace Sharebert.SearchLibrary
{
    /// <summary>
    /// ClientHandler provides an easy to use interface to reply to received messages.
    /// </summary>
    public class ClientHandler : IClientHandler
    {

        /// <summary>
        /// Initializes a new instance of the ClientHandler class.
        /// </summary>
        /// <param name="conn"></param>
        public ClientHandler(IConnection conn)
        {
            if (conn == null)
            {
                throw new ArgumentNullException("Connection can not be null");
            }
            this.connection = conn;
        }

        /// <summary>
        /// Sends the search results to the client.
        /// </summary>
        /// <param name="results">A list of search results</param>
        /// <exception cref="Sharebert.SearchLibrary.SerializationException">Serialization of the search results failed</exception>
        /// <exception cref="Sharebert.SearchLibrary.NetworkException">Error while sending the search results</exception>
        public void SendResult(SearchResult[] results)
        {
            if (results == null)
            {
                results = new SearchResult[1];
            }

            SearchResultList list = new SearchResultList(results);
            byte[] data = Serialisation.Serialize(list, typeof(SearchResultList));
            Header header = new Header(Message.COMMAND_SEARCH_RESPONSE, data.Length);
            IMessage responseMessage = new Message(header, data);

            this.connection.SendMessage(responseMessage);
        }

        /// <summary>
        /// Closes the underlying network stream and socket.
        /// </summary>
        public void Disconnect()
        {
            this.connection.Close();
        }

        /// <summary>
        /// Returns the remote [IP]:[Port]
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.RemoteIP + ":" + this.RemotePort;
        }


        #region Properties and private member

        public IPAddress RemoteIP { get { return IPAddress.Parse(this.connection.RemoteAddress); } }

        public int RemotePort { get { return this.connection.RemotePort; } }

        public IPAddress LocalIP { get { return IPAddress.Parse(this.connection.LocalAddress); } }

        public int LocalPort { get { return this.connection.LocalPort; } }

        private IConnection connection;

        #endregion

    }
}
